import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:thinking/config/_.dart';
import 'package:thinking/entity/_.dart';
class DynamicData extends StatefulWidget {
  final DynamicDataEntity dynamicData;
  final Function onShare;
  final Function onLike;

  DynamicData(
      {
        required this.dynamicData,
        required this.onLike,
        required this.onShare,
});

  @override
  _DynamicData createState() => _DynamicData(
    dynamicData: this.dynamicData,
      onLike: this.onLike,
      onShare: this.onShare,
);
}

class _DynamicData extends State<DynamicData> with SingleTickerProviderStateMixin {
  DynamicDataEntity dynamicData;
  final Function onShare;
  final Function onLike;
 late AnimationController _animationController;
 late Animation<double> _iconAnimation;

  _DynamicData(
      { required this.dynamicData, required this.onLike, required this.onShare});

  @override
  void initState() {
    super.initState();
    setLikeIconAnimated();
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  setLikeIconAnimated() {
    _animationController =
        AnimationController(duration: Duration(milliseconds: 300), vsync: this);
    _iconAnimation = TweenSequence([
      TweenSequenceItem(
          tween: Tween(begin: 1.0, end: 1.3)
              .chain(CurveTween(curve: Curves.easeIn)),
          weight: 50),
      TweenSequenceItem(tween: Tween(begin: 1.3, end: 1.0), weight: 50),
    ]).animate(_animationController);
  }

  Future<void> likeThisDynamic() async{
    if (_iconAnimation.status == AnimationStatus.forward ||
        _iconAnimation.status == AnimationStatus.reverse) {
      return;
    }
    dynamicData.setDynamicIsLike = !dynamicData.dynamicIsLike!;
    await onLike(dynamicData.dynamicIsLike);
    setState(() {
      if(dynamicData.dynamicIsLike!){
        BotToast.showText(text: '点赞成功');
        dynamicData.setDynamicLikeCount =dynamicData.dynamicLikeCount! +1;
      }else{
        dynamicData.setDynamicLikeCount =dynamicData.dynamicLikeCount! -1;
      }
    });
    if (_iconAnimation.status == AnimationStatus.dismissed) {
      _animationController.forward();
    } else if (_iconAnimation.status == AnimationStatus.completed) {
      _animationController.reverse();
    }

  }

  @override
  Widget build(BuildContext context) {
    return Row(
        children: [
          Expanded(
            flex: 1,
            child: TextButton(
              style: ButtonStyle(
                foregroundColor:
                MaterialStateProperty.resolveWith((states) {
                  return states.contains(MaterialState.pressed)
                      ? ColorConfig.ThemeOtherColor
                      : ColorConfig.TextColor;
                }),
              ),
              onPressed: () {
                onShare();
              },
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(
                    Icons.near_me_outlined,
                    size: 18,
                  ),
                  SizedBox(
                    width: 5,
                  ),
                  Text(dynamicData.dynamicShareCount == 0?'分享':dynamicData.dynamicShareCount.toString())
                ],
              ),
            ),
          ),
          Expanded(
            flex: 1,
            child: TextButton(
              style: ButtonStyle(
                foregroundColor:
                MaterialStateProperty.resolveWith((states) {
                  return states.contains(MaterialState.pressed)
                      ? ColorConfig.ThemeOtherColor
                      : ColorConfig.TextColor;
                }),
              ),

              onPressed: () {
                Modular.to.pushNamed('/dynamicInfo');
              },
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(
                    Icons.chat_bubble_outline,
                    size: 18,
                  ),
                  SizedBox(
                    width: 5,
                  ),
                  Text(dynamicData.dynamicCommentCount == 0?'评论':dynamicData.dynamicCommentCount.toString())
                ],
              ),
            ),
          ),
          Expanded(
            flex: 1,
              child: GestureDetector(
                onTap: () async{
                 await likeThisDynamic();
                },
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    ScaleTransition(
                      scale: _iconAnimation,
                      child: dynamicData.dynamicIsLike!
                          ? Icon(
                        CupertinoIcons.heart_fill,
                        size: 18,
                        color: ColorConfig.ThemeColor,
                      )
                          : Icon(
                        CupertinoIcons.heart,
                        size: 18,
                        color: ColorConfig.TextColor,
                      ),
                    ),
                    SizedBox(
                      width: 5,
                    ),
                    Text(dynamicData.dynamicLikeCount == 0?'点赞':dynamicData.dynamicLikeCount.toString(),style: TextStyle(color:dynamicData.dynamicIsLike! ? ColorConfig.ThemeColor
                        : ColorConfig.TextColor),)
                  ],
                ),
              ),
          ),
        ],
    );
  }
}
